import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:window_manager/window_manager.dart';
import 'package:get/get.dart';
import 'package:window_manager/window_manager.dart';
import 'package:toastification/toastification.dart';

import '../common/app.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WindowListener {
  final GlobalKey globalKey = GlobalKey();
  var size;
  var width;
  var height;

  _getWH() {
    final containerWidth = globalKey.currentContext!.size!.width;
    final containerHeight = globalKey.currentContext!.size!.height;
    print('Container widht is $containerWidth, height is $containerHeight');
    return {
      'containerWidth': containerWidth,
      'ontainerHeight': containerHeight
    };
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    // ScreenUtil().screenWidth;
    // ScreenUtil().screenHeight;
    // ScreenUtil().statusBarHeight;
    // AppBar().preferredSize.height;
    // ScreenUtil().bottomBarHeight;
    AppBar appBar = AppBar(
      elevation: 0,
      centerTitle: true,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            color: Colors.cyanAccent,
            child: const Text('appbar'),
          )
        ],
      ),
      systemOverlayStyle: const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          systemNavigationBarContrastEnforced: true),
      backgroundColor: Colors.redAccent,
    );
    return Scaffold(
      appBar: appBar,
      body: SafeArea(
        top: true,
        bottom: true,
        child: Wrap(
          direction: Axis.horizontal,
          children: [
            Container(
              key: globalKey,
              //margin: const EdgeInsets.all(3),
              height: App().windowHeight -
                  ScreenUtil().statusBarHeight -
                  AppBar().preferredSize.height -
                  ScreenUtil().bottomBarHeight -
                  App().titleBarHeight,
              width: App().windowWidth / 3,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.blueGrey, width: 2.0),
                borderRadius: const BorderRadius.all(Radius.circular(3.0)),
              ),
              child: Column(
                children: [
                  Text('App().windowHeight:${App().windowHeight}'),
                  Text('App().windowWidth:${App().windowWidth}'),
                  Text('App().titleBarHeight：${App().titleBarHeight}'),
                  Text(
                      'ScreenUtil().screenHeight:${ScreenUtil().screenHeight}'),
                  Text('ScreenUtil().screenWidth:${ScreenUtil().screenWidth}'),
                  Text(
                      'ScreenUtil().screenHeight:${ScreenUtil().screenHeight}'),
                  Text(
                      'ScreenUtil().statusBarHeight:${ScreenUtil().statusBarHeight}'),
                  Text(
                      'AppBar().preferredSize.height:${AppBar().preferredSize.height}'),
                  Text(
                      'ScreenUtil().bottomBarHeight:${ScreenUtil().bottomBarHeight}'),
                  Text('size:$size,height:$height,width:$width'),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        size = globalKey.currentContext?.size;
                        width = globalKey.currentContext?.size?.width;
                        height = globalKey.currentContext?.size?.height;
                      });
                    },
                    child: const Text('getHight'),
                  ),
                ],
              ),
            ),
            Container(
              //margin: const EdgeInsets.all(3),
              height: App().windowHeight,
              width: App().windowWidth / 3,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.blueGrey, width: 2.0),
                borderRadius: const BorderRadius.all(Radius.circular(3.0)),
              ),
              child: Column(
                children: [
                  Text('size:$size,height:$height,width:$width'),
                  ElevatedButton(
                    onPressed: () {
                      setState(() {
                        size = globalKey.currentContext?.size;
                        width = globalKey.currentContext?.size?.width;
                        height = globalKey.currentContext?.size?.height;
                      });
                    },
                    child: const Text('getHight'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      ToastificationItem toastificationItem =toastification.show(
                        context: context,
                        title: Text('Hello, world!'),
                        alignment: Alignment.center,
                      );
                      Future.delayed(Duration(seconds: 30),(){toastification.dismiss(toastificationItem);});
                    },
                    child: const Text('toastification.show'),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }
}
